package sql.ddl;

import java.util.ArrayList;

import sql.ddl.coluna.Coluna;
import sql.ddl.coluna.ColunaComposta;
import sql.ddl.coluna.ListaColuna;

public class PrimaryKey implements TokensSQL {
	ArrayList<Coluna> colunas = null;

	String constraint = null;

	public PrimaryKey(Coluna coluna) {
		colunas = new ArrayList<Coluna>();
		colunas.add(coluna);
	}
	public PrimaryKey(ArrayList<Coluna> colunas) {
		this.colunas = colunas;
	}
	public PrimaryKey(ListaColuna clns, String constraint) {
		this(clns);
		this.constraint = constraint;
	}

	public String codigoSQL() {
		return constraint() + PRIMARY + SPACE + KEY + BEGINPARENTES + colunas()
				+ ENDPARENTES;
	}

	private String constraint() {
		return constraint != null ? CONSTRAINT + SPACE + constraint + SPACE
				: NONVALUE;
	}

	private String colunas() {
		String s = lerNomeColuna(colunas.get(0));
		for (int i = 1; i < colunas.size(); i++) {
			s += VIRGUL + lerNomeColuna(colunas.get(i));
		}
		return s;
	}

	private String lerNomeColuna(Coluna coluna) {
		return coluna instanceof ColunaComposta?((ColunaComposta)coluna).nomeColunas():coluna.getNome();

	}
	public void setConstraint(String alias) {
		this.constraint = alias;
	}

	public ArrayList<Coluna> getColunas() {
		return this.colunas;
	}
	
	
	
}
